﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace Quản_Lý_Thư_Viện
{
    class PhieumuonDataAccess
    {
        private DataProvider _provider = new DataProvider();
        public PhieumuonDataAccess()
        {
            _provider.connect();
        }
        int i;
        DataTable dt = new DataTable();
        public void insertphieumuon(PhieumuonInfo info)
        {
            NhanvienControl control = new NhanvienControl();
            NhanvienInfo nvinfo = new NhanvienInfo();
            DataTable dt2 = new DataTable();
            dt2 = control.NhanvienDangnhap();
            DataRow dr = dt2.Rows[0];
            dt = laydsphieumuon();
            i = dt.Rows.Count;

            string ngaymuon = info.Ngmuon.Value.Month + "/" + info.Ngmuon.Value.Day + "/" + info.Ngmuon.Value.Year + " " + info.Ngmuon.Value.Hour + ":" + info.Ngmuon.Value.Minute + ":" + info.Ngmuon.Value.Second;
            string ngaytraqd = info.Ngaytraqd.Value.Month + "/" + info.Ngaytraqd.Value.Day + "/" + info.Ngaytraqd.Value.Year;
            string insertCommand = "insert into dbo.PHIEUMUONSACH values('PM" + i + "','" + frmMuonSach.madg + "','" + dr["MANV"].ToString() + "','" + ngaymuon + "','"+ngaytraqd+"')";
            _provider.executeNonQuery(insertCommand);
        }
        public DataTable laydsphieumuon()
        {
            return _provider.GetData("select * from dbo.PHIEUMUONSACH order by NGMUON asc");
        }
        public DataTable sachchoctpm()
        {
            DataTable dttemp = new DataTable();
            dttemp = laydsphieumuon();
            DataRow dr = dttemp.Rows[i];
            string temp = dr["MAPMS"].ToString();
            return _provider.GetData("select * from dbo.PHIEUMUONSACH where MAPMS = '"+temp+"'");
        }
        public DataTable madgpts()
        {
            return _provider.GetData("Select MADG from dbo.PHIEUMUONSACH,dbo.CT_PMS where dbo.PHIEUMUONSACH.MAPMS = dbo.CT_PMS.MAPMS and dbo.PHIEUMUONSACH.MAPMS = '" + frmPhieuTraSach.mapmstemp + "' ");

        }
        public DataTable ngaymuon()
        {
            return _provider.GetData("Select NGMUON from dbo.PHIEUMUONSACH where dbo.PHIEUMUONSACH.MAPMS = '" + frmPhieuTraSach.mapmstemp + "'");
        }
        public void ghsachpms()
        {
            string updatecmd = ("update dbo.phieumuonsach set ngtraqd = dateadd(day," + frmGiaHan.songaygh + ",ngtraqd) where dbo.phieumuonsach.mapms = '" + frmGiaHan.mapm + "'");
            _provider.executeNonQuery(updatecmd);
        }
        public void ghsach()
        {
            string updatecommand = ("update dbo.PHIEUMUONSACH set NGTRAQD = dateadd(DAY," + frmGiaHan.songaygh + ",NGTRAQD)  where (CONVERT(varchar,dbo.phieumuonsach.NGTRAQD, 103) = '" + frmGiaHan.dtpgiahansach + "' and dbo.phieumuonsach.madg = '" + frmGiaHan.madg + "' )");
            _provider.executeNonQuery(updatecommand);
        }
    }
}
